<?php

namespace app\admin\controller;
use app\admin\model\Member as MembeModel;
use app\admin\model\MemberArithmeticalPower as MemberArithmeticalPowerModel;
use app\admin\model\MiningArithmetical as MiningArithmeticalModel;
use app\admin\model\Mining as MiningModel;

class MemberArithmeticalPower extends Admin
{
    private $postField = 'surplus_arithmetical,arithmetical_power,pledge_money,return_pledge,pay_status';

    function index(){
        if (!$this->request->isPost()){
            return view('index');
        }else{
            $limit  = $this->request->post('limit', 20, 'intval');
            $page = $this->request->post('page', 1, 'intval');
            $order  = $this->request->post('order', '', 'serach_in');	//排序字段
            $sort  = $this->request->post('sort', '', 'serach_in');		//排序方式

            $orderby = ($sort && $order) ? $sort.' '.$order : 'id desc';

            //查询所有套餐
            $MiningArithmetical = MiningArithmeticalModel::getListAll();
            $MiningArithmeticalAll = [];
            foreach ($MiningArithmetical as $v){
                $MiningArithmeticalAll[$v['id']] = $v;
            }

            //获取算力矿机订单
            $res = MemberArithmeticalPowerModel::getList($page,$limit,[],$order);
            foreach ($res['data'] as $k => &$v){
                $member = MembeModel::getDetails($v['mid']);
                $v['user'] = $member['username'].' | '.$v['mid'];
                $v['mining_arithmetical'] = !empty($MiningArithmeticalAll[$v['maid']]) ? $MiningArithmeticalAll[$v['maid']]['name'] : '套餐id:'.$v['maid'].'(已被删除)';
            }

            $data['status'] = 200;
            $data['data'] = $res;
            return json($data);
        }
    }

    /**
     * 获取该矿机详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getEdit(){
        $id =  $this->request->post('id', '', 'serach_in');
        if(!$id) throw new ValidateException ('参数错误');
        $res = MemberArithmeticalPowerModel::getDetails($id);
        return json(['status'=>200,'data'=>$res]);
    }

    /**
     * 修改矿机
     * @return \think\response\Json
     */
    function edit(){
        $postField = $this->postField.',id';
        $data = $this->request->only(explode(',',$postField),'post',null);
        $data['update_time'] = date('Y-m-d H:i:s',time());
        MemberArithmeticalPowerModel::where('id',$data['id'])->update($data);
        return json(['status'=>200,'msg'=>'修改成功']);
    }

    /**
     * 删除矿机
     * @return \think\response\Json
     */
    function delete(){
        $idx =  $this->request->post('id', '', 'serach_in');
        if(!$idx) throw new ValidateException ('参数错误');
        MemberArithmeticalPowerModel::destroy(['id'=>explode(',',$idx)],true);
        return json(['status'=>200,'msg'=>'操作成功']);
    }

}